package com.zzg.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzg.springboot.dao.ProductDao;
import com.zzg.springboot.dao.ProductTypeDao;
import com.zzg.springboot.entity.Product;
import com.zzg.springboot.entity.ProductType;

@Controller
@RequestMapping("/api/springboot")
public class ProductTypeController {
	@Autowired
	private ProductDao productDao;
	@Autowired
	private ProductTypeDao productTypeDao;

	@RequestMapping("/one")
	public void one() {
		ProductType type = new ProductType();
		type.setName("商品类型类型1");

		Product product1 = new Product("商品1");
		Product product2 = new Product("商品2");

		// 只能从一方建立到多方的关系
		type.getProducts().add(product1);
		type.getProducts().add(product2);// 临时状态
		
		productDao.save(product1);
		productDao.save(product2);
		productTypeDao.save(type);
	}
	
	@RequestMapping("/two")
	@ResponseBody
	public ProductType two(@RequestParam Integer id){
		ProductType productType = productTypeDao.findOne(id);
		return productType;
	}

}
